package com.example.edu.service.impl;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.client.VodClient;
import com.example.edu.entity.frontvo.CourseQueryVo;
import com.example.edu.entity.frontvo.CourseWebVo;
import com.example.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.edu.entity.Chapter;
import com.example.edu.entity.Course;
import com.example.edu.entity.CourseDescription;
import com.example.edu.entity.Video;
import com.example.edu.entity.excel.CoursePublishInfo;
import com.example.edu.entity.excel.CourseVoco;
import com.example.edu.exceptionhandler.HeartEduException;
import com.example.edu.mapper.ChapterMapper;
import com.example.edu.mapper.CourseDescriptionMapper;
import com.example.edu.mapper.CourseMapper;
import com.example.edu.service.CourseService;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-02
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

	@Autowired
	VodClient vodClient;

	@Autowired
	private VideoService videoService;
	
	@Autowired
	private ChapterMapper chapterMapper;

	@Autowired
	private CourseDescriptionMapper courseDescriptionMapper;
	public String saveCourse(CourseVoco courseVoco) {
		// TODO Auto-generated method stub
		System.out.println("ccc");
		Course course = new Course();
		BeanUtils.copyProperties(courseVoco, course);
		System.out.println(course);
		System.out.println(course.getId()+"画画");
		baseMapper.insert(course);
//		System.out.println(course.getId());
		String cid = course.getId();
		CourseDescription courseDescription = new CourseDescription();
		courseDescription.setDescription(courseVoco.getDescription());
		courseDescription.setId(course.getId());
		courseDescriptionMapper.insert(courseDescription);
		
		return cid;
		
	}
	@Override
	public CourseVoco getCourseVoco(String id) {
		// TODO Auto-generated method stub
		
		Course course = baseMapper.selectById(id);
		
		CourseDescription courseDescription = courseDescriptionMapper.selectById(id);
		
		CourseVoco coursevoco = new CourseVoco();
		BeanUtils.copyProperties(course, coursevoco);
		
		BeanUtils.copyProperties(courseDescription, coursevoco);
		
		return coursevoco;
	}

	@Override
	public void updateCourseVoco(CourseVoco courseVoco) {
		// TODO Auto-generated method stub
		Course course = new Course();
		BeanUtils.copyProperties(courseVoco, course);
		int i = baseMapper.updateById(course);
		if(i==0) {
			throw new HeartEduException(20001,"更新课程信息失败");
		}
		CourseDescription courseDescription = new CourseDescription();
		courseDescription.setId(courseVoco.getId());
		courseDescription.setDescription(courseVoco.getDescription());
		
		int j = courseDescriptionMapper.updateById(courseDescription);
		if(j==0) {
			throw new HeartEduException(20001,"更新课程描述失败");
		}
	}
	@Override
	public CoursePublishInfo getCoursePublishInfo(String id) {
		// TODO Auto-generated method stub
		CoursePublishInfo coursePublishInfo = baseMapper.getCoursePublishInfo(id);
		return coursePublishInfo;
	}
	@Override
	public void publishCourse(String id) {
		// TODO Auto-generated method stub
		Course course = new Course();
		course.setId(id);
		course.setStatus("Normal");
	    this.updateById(course);
	}
	@Override
	public List<CoursePublishInfo> getAllCoursePublishInfo() {
		// TODO Auto-generated method stub
		
		List<CoursePublishInfo> courseAllPublishInfo = baseMapper.getAllCoursePublishInfo();
		return courseAllPublishInfo;
	}
	@Override
	public List<CoursePublishInfo> getAllCoursePublishInfoByMap(Map<String, Object> map) {
        //TODO Auto-generated method stub

		
		List<CoursePublishInfo> courseAllPublishInfo = baseMapper.getAllCoursePublishInfoByMap(map);
		return courseAllPublishInfo;
	}
	@Override
	public Integer selectCount(Map<String,Object> map) {
		// TODO Auto-generated method stub
		Integer count =  baseMapper.selectCount(map);
		return count;
	}

	@Override
	public void deleteCoursePublishInfo(String courseId) {
		// TODO Auto-generated method stub
		//1.删除视频
		QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
		videoQueryWrapper.eq("course_id",courseId);
		videoQueryWrapper.select("video_source_id");
		List<Video> videoList = videoService.list(videoQueryWrapper);

		List<String> videoSourceIdList = new ArrayList<>();
		for (int i = 0; i < videoList.size(); i++) {
			Video video = videoList.get(i);
			String videoSourceId = video.getVideoSourceId();
			if(!StringUtils.isEmpty(videoSourceId)){
				videoSourceIdList.add(videoSourceId);
			}
		}
		vodClient.deleteVideos(videoSourceIdList);

		//2.删除小节
		QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
		videoWrapper.eq("course_id", courseId);
		videoService.remove(videoWrapper);
		
		//3.修改章节
		QueryWrapper<Chapter> chapterWrapper = new QueryWrapper<>();
		chapterWrapper.eq("course_id", courseId);
		chapterMapper.delete(chapterWrapper);
		//4.修改描述
		courseDescriptionMapper.deleteById(courseId);
		
		baseMapper.deleteById(courseId);
		
	}
	
	@Cacheable(key = "course",value = "courseList")
	@Override
	public List<Course> selectAllCourse() {
		// TODO Auto-generated method stub
		
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        
        courseQueryWrapper.orderByDesc("id");
        courseQueryWrapper.last("limit 8");
        List<Course> courseList = baseMapper.selectList(courseQueryWrapper);
		return courseList;
	}

	@Override
	public Map<String, Object> getFrontCourse(Page<Course> coursePage, CourseQueryVo courseQueryVo) {
		QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
		if(!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())){
			queryWrapper.eq("subject_parent_id",courseQueryVo.getSubjectParentId());
		}

		if(!StringUtils.isEmpty(courseQueryVo.getSubjectId())){
			queryWrapper.eq("subject_id",courseQueryVo.getSubjectId());
		}
		if(!StringUtils.isEmpty(courseQueryVo.getBuyCountSort())){
			queryWrapper.orderByDesc("buy_count");
		}
		if(!StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())){
			queryWrapper.orderByDesc("gmt_create");
		}
		if(!StringUtils.isEmpty(courseQueryVo.getPriceSort())){
			queryWrapper.orderByDesc("price");
		}

		baseMapper.selectPage(coursePage, queryWrapper);
		List<Course> courseList = coursePage.getRecords();
		long current = coursePage.getCurrent();
		long size = coursePage.getSize();
		long total = coursePage.getTotal();
		long pages = coursePage.getPages();

		boolean hasNext = coursePage.hasNext();
		boolean hasPrevious = coursePage.hasPrevious();

		Map<String,Object> map = new HashMap<>();
		map.put("courseList", courseList);
		map.put("current", current);
		map.put("size", size);
		map.put("total", total);
		map.put("pages", pages);
		map.put("hasNext", hasNext);
		map.put("hasPrevious",hasPrevious);


		return map;
	}

	@Override
	public CourseWebVo getFrontCourseInfoById(String courseId) {

		CourseWebVo courseWebVo = baseMapper.getFrontCourseInfoById(courseId);

		return courseWebVo;
	}


}
